# -*- coding: utf-8 -*-
# @Date   : 2020/01/08
# @File   : IC_UpaidLoanCnt_ZBC.py
# @Author : yanhaopeng

'''eof
name:未结清贷款笔数
code:IC_UpaidLoanCnt_ZBC
tableName:
columnName:
groups:征信模块-准入策略
dependencies:IC_DSJ_2D
type:常用指标
dataSourceType:在线指标
description:
eof'''

'''
Factor = 非循环贷账户汇总信息段(PC02E)·账户数(PC02ES02)
        +循环额度下分账户汇总信息段(PC02F)中·账户数(PC02FS02)
        +循环贷账户汇总信息段(PC02G)·账户数(PC02GS02)
'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']


def getTable(data, tableName, occurTimes):
    '''
        获取表信息
        :param data:表上层的字典对象
        :param tableName:表的名字
        :param occurTimes:表出现的次数，如果是0代表此表可以不出现，如果是1代表此表必须出现
        :return:该表全部数据
        '''
    table = data.get(tableName)
    if occurTimes == 0:
        if table is None or table == "":
            return [{}]
        elif isinstance(table, list) is False or len(table) == 0:
            return None
        else:
            return data.get(tableName)
    if occurTimes == 1:
        if table == "":
            return [{}]
        if table is None or isinstance(table, list) is False or len(table) == 0:
            return None
        elif len(table) == 0:
            return [{}]
        else:
            return data.get(tableName)


def isfloat(x):
    '''
    判断x为float类型且大于等于0
    :param x:
    :return:
    '''
    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def IC_UpaidLoanCnt():
    data = IC_DSJ_2D['data']
    factor = []
    #信贷交易信息概要   [1..1]
    PCO = getTable(data, 'PCO', 1)
    if PCO is None:
        return u'缺失值'
    elif PCO == [{}]:
        return 0
    else:
        #借贷交易信息概要信息单元   [0..1]
        PC02 = getTable(PCO[0], 'PC02', 0)
        if PC02 is None:
            return u'缺失值'
        elif PC02 == [{}]:
            return 0
        else:
            # 非循环贷账户汇总信息段   [0..1]
            PC02E = getTable(PC02[0], 'PC02E', 0)
            #循环额度下分账户汇总信息段   [0..1]
            PC02F = getTable(PC02[0], 'PC02F', 0)
            #循环贷账户汇总信息段   [0..1]
            PC02G = getTable(PC02[0], 'PC02G', 0)
            if PC02E is None:
                factor.append(None)
            elif PC02E == [{}]:
                factor.append(0)
            else:
                #账户数   [1..1]
                PC02ES02 = PC02E[0].get('PC02ES02')
                if PC02ES02 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02ES02):
                        factor.append(float(PC02ES02))
                    else:
                        factor.append(None)
            if PC02F is None:
                factor.append(None)
            elif PC02F == [{}]:
                factor.append(0)
            else:
                #账户数   [1..1]
                PC02FS02 = PC02F[0].get('PC02FS02')
                if PC02FS02 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02FS02):
                        factor.append(float(PC02FS02))
                    else:
                        factor.append(None)
            if PC02G is None:
                factor.append(None)
            elif PC02G == [{}]:
                factor.append(0)
            else:
                #账户数   [1..1]
                PC02GS02 = PC02G[0].get('PC02GS02')
                if PC02GS02 in null_type_list:
                    factor.append(None)
                else:
                    if isfloat(PC02GS02):
                        factor.append(float(PC02GS02))
                    else:
                        factor.append(None)


            factor = filter(lambda x: x is not None, factor)
            if len(factor) == 0:
                return u'缺失值'
            else:
                return sum(factor)

result = IC_UpaidLoanCnt()

# import json
# ratingRequest = {"legalPerson": u"朱为广", "legalPersonIdCard": "510102196706108511", "creditCode": "123456",
#                  "regNumber": "12345", "relativeDate":u'2019/08/09'}
# with open('F:/hp/IC_DSJ1.json', 'r') as f:
#     IC_DSJ_2D = json.loads(f.read())['IC_DSJ']
# print IC_UpaidLoanCnt()